//
//  YTStashRecordsDetailsViewController.m
//  YuTang
//
//  Created by 葛悦彣 on 2024/11/18.
//

#import "YTStashRecordsDetailsViewController.h"
#import "YTStashRecordModel.h"
#import "YTStashRecordsDetailsHeaderView.h"
#import "YTStashRecordsDetailsTableViewCell.h"

@interface YTStashRecordsDetailsViewController ()
@property (nonatomic ,strong) YTStashRecordModel * model;
@property (nonatomic ,strong) UILabel * leftlab;
@end

@implementation YTStashRecordsDetailsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = KcolorHex(@"#FBFBFB");
    
    UIView * topview = [UIView new];
    topview.backgroundColor = KcolorHex(@"#0967FF");
    [self.view addSubview:topview];
    [topview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.offset(0);
        make.height.offset(Knav_HeightX);
    }];
        
    MJWeakSelf
    UIView * backview = [UIView new].addClickBlock(^{
        [weakSelf popViewController];
    });
    [topview addSubview:backview];
    [backview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.offset(44);
        make.left.bottom.offset(0);
        make.width.offset(kwt(120));
    }];
    
    
    
    UIImageView * leftimg = kimageView(@"white_path");
    [backview addSubview:leftimg];
    [leftimg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.offset(20);
        make.left.offset(kwt(10));
        make.centerY.offset(0);
    }];
    
    UILabel * leftlab = [DTKTool labelWithText:@"出入库记录" andFont:kCFont(19) andTextColor:UIColor.whiteColor];
    [backview addSubview:leftlab];
    self.leftlab = leftlab;
    [leftlab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(43));
        make.centerY.offset(0);
    }];
    
    [self.view addSubview:self.tableview];
    self.tableview.backgroundColor = KcolorHex(@"#ECEDEE");
    [self.tableview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.offset(0);
        make.top.equalTo(topview.mas_bottom);
    }];
    [self loadNewData];
}
- (void)loadNewData{
    [self.view showBusyHUD];
    NSMutableDictionary * dict = [NSMutableDictionary dictionary];
    [dict setObject:self.ID forKey:@"id"];
    [SCNetManger warehouseBoundDetailWithDict:dict andCompetionHandlerBlock:^(HTTPDicBase * model) {
        [self.view hideBusyHUD];
        if(model.code == 200){
            self.model = [YTStashRecordModel yy_modelWithDictionary:model.result];
            if(self.model.type == 1){
                self.leftlab.text = @"出库记录";
            } else if(self.model.type == 0){
                self.leftlab.text = @"入库记录";
            }
            [self.tableview reloadData];
        } else {
            [self.view showWarning:model.message];
        }
    }];
}


-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if(self.model){
        YTStashRecordsDetailsHeaderView * headerview = [YTStashRecordsDetailsHeaderView initview];
        headerview.model = self.model;
        return headerview;
    } else {
        return [UIView new];
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if(self.model){
        return self.model.HeaderViewH;
    }
    return 0;
}

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return  1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if(self.model){
        return self.model.CellH;
    }
    return 0;

}

- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    
    
    YTStashRecordsDetailsTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"YTStashRecordsDetailsTableViewCell"];
    if (cell == nil) {
        cell = [[YTStashRecordsDetailsTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"YTStashRecordsDetailsTableViewCell"];
    }
    cell.model = self.model;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell ;
    
}
@end
